package cfca.paperless.client.servlet.customer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;
import cfca.paperless.client.util.StringUtil;

/**
 * 佰仟金融无纸化（挑战码）性能测试
 * 一码一签、一码多签
 * 
 * @author clsu
 *
 */
public class testCompoundSealAutoPdfList_billionsfinance1 {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
            
            ///////////////////////////如下为用户1的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
            byte[] handwritingImage1 = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));//加载手写签名图片
            byte[] handwritingData1 = handwritingImage1;//加载手写签名图片数据
            byte[] identificationFrontData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));//加载场景照片
            byte[] identificationBackData = FileUtils.readFileToByteArray(new File("./TestData/2.png"));//加载场景照片

            List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
            list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "descr1", "2"));
            list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));
            list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "0"));
            list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "0"));

            List<SignLocation> signLocationList = new ArrayList<SignLocation>();
            signLocationList.add(new SignLocation(-1, "代理人签名", "R", 10, 0));/// 表示在第一个pdf上的“交易柜员”关键字中间向上偏移50px后盖章

            String proofHashAndMultiData1 [] =PaperlessUtil.getProofHashAndMultiData(list1);

            SealUserInfo sealUserInfo = new SealUserInfo("张三", "Z", "ABC1DDD111", "14866659475", "beijing", "reason");//用户的信息，用来申请证书用
            //产生证据策略文件并添加到clientBean中，此策略文件后台会申请场景证书用来盖章
            String proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo, signLocationList, proofHashAndMultiData1[0]);
            String sealLayer2TextStyle = "font-size:10;";
            proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml("张三丰", sealLayer2TextStyle, sealUserInfo, signLocationList, proofHashAndMultiData1[0]);
            ///////////////////////////以上为用户1的数据/////////////////////////////////////////////////
            
            ////--------------------------------------------------------------------------------------//////

            String pdf1 = Base64.toBase64String(FileUtils.readFileToByteArray(new File("./TestData/file.pdf")));
            
            String bizTypeCode = "B001";//业务类型，比如：开户、开通网银
            
            List<PdfBean> pdfBeanList = new ArrayList<PdfBean>();
            String outputFilePath = "auto";
            String bizSerialNo = GUID.generateId();
            PdfBean pdfBean = new PdfBean(pdf1, bizSerialNo, bizTypeCode, outputFilePath);
            pdfBean.setReturnPdfOrNot(true);//可以指定不返回pdf,默认返回pdf
            pdfBeanList.add(pdfBean);
            PdfBean pdfBean2 = new PdfBean(pdf1, bizSerialNo, bizTypeCode, outputFilePath);
            pdfBeanList.add(pdfBean2);
            
            String pdfBeanListXml = PaperlessUtil.buildPdfBeanListXml(pdfBeanList);

            //调用自动化复合签章pdf列表方法
            String multiData = "[" + proofHashAndMultiData1[1] +  "]";
            String compoundSealStrategyXml = "<List>" + proofSealStrategyXml1 + "</List>";
            String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();
            String sealedPdf = clientBean.compoundSealAutoPdfList(pdfBeanListXml, multiData, compoundSealStrategyXml, PaperlessConfig.operatorCode, channelBeanXml);
            String code = StringUtil.getNodeText(sealedPdf, "Code");
            //返回值Code等于200代表后台处理成功
            if (code.equals("200")) {
                List<String[]> list = getPdfBeanArr(sealedPdf);
                for (int i = 0; i < list.size(); i++) {
                    String[] strArr = list.get(i);
                    if (pdfBean.isReturnPdfOrNot()) {
                        IoUtil.write("./TestData/output/compoundSealAutoPdfList-" + i + ".pdf", Base64.decode(strArr[0].getBytes()));
                        System.out.println("bizSerialNo=" + strArr[1]);
                        System.out.println("proofPdfId=" + strArr[2]);
                        
                    } else {
                        System.out.println("bizSerialNo=" + strArr[0]);
                        System.out.println("proofPdfId=" + strArr[1]);
                    }
                }

            } else {
                System.out.println(sealedPdf);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("time:" + (System.currentTimeMillis() - start));
    }

    private static List<String[]> getPdfBeanArr(String pdfs) {
        List<String[]> list = new ArrayList<String[]>();
        Object pdfArr[] = StringUtil.getXmlFieldArr(pdfs, "PdfBean");
        String authCode = StringUtil.getXmlField(pdfs, "AuthCode");
        System.out.println("授权码：" + authCode);
        if (pdfArr != null && pdfArr.length > 0) {
            for (int i = 0; i < pdfArr.length; i++) {
                String pdfBeanString = "<PdfBean>" + pdfArr[i].toString() + "</PdfBean>";
                String pdf = StringUtil.getXmlField(pdfBeanString, "Pdf");
                String bizSerialNo = StringUtil.getXmlField(pdfBeanString, "BizSerialNo");
                if (StringUtil.isEmpty(bizSerialNo)) {
                    bizSerialNo = "";
                }
                String proofPdfId = StringUtil.getXmlField(pdfBeanString, "ProofPdfId");
                if (StringUtil.isEmpty(proofPdfId)) {
                    proofPdfId = "";
                }

                list.add(new String[] { pdf, bizSerialNo, proofPdfId});
            }
        }
        return list;
    }
    

}
